home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
By the Book
/
Learn C++ (CodeWarrior)
/
Chap 07.02 - menu
/
menu.cp
< prev
next >
Wrap
Text File
|
1995-10-20
|
2KB
|
84 lines
#include <iostream.h>
#include <string.h>
const short kMaxNameLength = 40;
//--------------------------------------- MenuItem
class MenuItem
{
private:
float price;
char name[ kMaxNameLength ];
public:
MenuItem( float itemPrice, char *itemName );
float GetPrice();
float operator+( MenuItem item );
float operator+( float subtotal );
};
MenuItem::MenuItem( float itemPrice, char *itemName )
{
price = itemPrice;
strcpy( name, itemName );
}
float MenuItem::GetPrice()
{
return( price );
}
float MenuItem::operator+( MenuItem item )
{
cout << "MenuItem::operator+( MenuItem item )\n";
return( GetPrice() + item.GetPrice() );
}
float MenuItem::operator+( float subtotal )
{
cout << "MenuItem::operator+( float subtotal )\n";
return( GetPrice() + subtotal );
}
float operator+( float subtotal, MenuItem item );
// I added the previous line, cause CodeWarrior reports (correctly)
// that there was no prototype for float operator+().
// Now there IS a prototype! Comment out the line
// if you want to see the warning message -- Dave Mark, 10/20/95
//--------------------------------------- operator+()
float operator+( float subtotal, MenuItem item )
{
cout << "operator+( float subtotal, MenuItem item )\n";
return( subtotal + item.GetPrice() );
}
//--------------------------------------- main()
int main()
{
MenuItem chicken( 8.99, "Chicken Kiev with salad" );
MenuItem houseWine( 2.99, "Riesling by the glass" );
MenuItem applePie( 3.99, "Apple Pie a la Mode" );
float total;
total = chicken + houseWine + applePie;
cout << "\nTotal: " << total
<< "\n\n";
total = chicken + (houseWine + applePie);
cout << "\nTotal: " << total;
return 0;
}